/**
 * Dear programmer:
 * When I wrote this code, only god and I knew how it worked.
 * Now, only god knows it!
 *
 * @author: Pegasus  Date: 2018-12-01 Time: 00:35
 *
 * Picker工具类
 */

import Picker from 'react-native-picker';

export const showDatePicker = () => {
  Picker.init({
    pickerData: _createDateData(),
    pickerFontColor: [255, 0, 0, 1],
    onPickerConfirm: (pickedValue, pickedIndex) => {
      console.log('date', pickedValue, pickedIndex);
    },
    onPickerCancel: (pickedValue, pickedIndex) => {
      console.log('date', pickedValue, pickedIndex);
    },
    onPickerSelect: (pickedValue, pickedIndex) => {
      console.log('date', pickedValue, pickedIndex);
    }
  });
  Picker.show();
}

const _createDateData = () => {
  let date = [];
  for (let i = 1970; i < 2020; i++) {
    let month = [];
    for (let j = 1; j < 13; j++) {
      let day = [];
      if (j === 2) {
        for (let k = 1; k < 29; k++) {
          day.push(k + '日');
        }
        //Leap day for years that are divisible by 4, such as 2000, 2004
        if (i % 4 === 0) {
          day.push(29 + '日');
        }
      } else if (j in {1: 1, 3: 1, 5: 1, 7: 1, 8: 1, 10: 1, 12: 1}) {
        for (let k = 1; k < 32; k++) {
          day.push(k + '日');
        }
      } else {
        for (let k = 1; k < 31; k++) {
          day.push(k + '日');
        }
      }
      let _month = {};
      _month[j + '月'] = day;
      month.push(_month);
    }
    let _date = {};
    _date[i + '年'] = month;
    date.push(_date);
  }
  return date;
}